#include "imgprocessor.h"

ImgProcessor::ImgProcessor(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle(tr("Easy Word"));
    showWidget = new ShowWidget(this);
    setCentralWidget(showWidget);

    //创建动作、菜单、工具栏的函数
    createActions();
    createMenus();
    createToolBars();
    if(img.load("image.png"));
    {
        //在ImgaeLabel对象中放置图片
        showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
    }

}

ImgProcessor::~ImgProcessor()
{
}

void ImgProcessor::createActions()
{
    //"打开"动作
    openFileAction = new QAction(QIcon("open.png"),tr("打开"),this);

    openFileAction->setShortcut(tr("Ctrl+O"));
    openFileAction->setStatusTip(tr("打开一个文件"));
    connect(openFileAction,SIGNAL(triggered()),this,SLOT(ShowOpenFile()));
    //新建动作
    NewFileAction = new QAction(QIcon("new.png"),tr("新建"),this);
    NewFileAction->setShortcut(tr("Ctrl+N"));
    NewFileAction->setStatusTip(tr("新建一个文件"));
    connect(NewFileAction,SIGNAL(triggered()),this,SLOT(ShowNewFile()));
    //退出动作
    exitAction = new QAction(tr("退出"),this);
    exitAction->setShortcut(tr("Ctrl+Q"));
    exitAction->setStatusTip(tr("退出程序"));
    connect(exitAction,SIGNAL(triggered()),this,SLOT(close()));

    //复制动作
    copyAction = new QAction(QIcon("copy.png"),tr("复制"),this);
    copyAction->setShortcut(tr("Ctrl+C"));
    copyAction->setStatusTip(tr("复制文件"));
    connect(copyAction,SIGNAL(triggered()),showWidget->text,SLOT(copy()));
    //剪切动作
    cutAction = new QAction(QIcon("cut.png"),tr("剪切"),this);
    cutAction->setShortcut(tr("Ctrl+X"));
    cutAction->setStatusTip(tr("剪切文件"));
    connect(cutAction,SIGNAL(triggered()),showWidget->text,SLOT(cut()));
    //粘贴动作
    pasteAction = new QAction(QIcon("paste.png"),tr("粘贴"),this);
    pasteAction->setShortcut(tr("Ctrl+V"));
    pasteAction->setStatusTip(tr("粘贴文件"));
    connect(pasteAction,SIGNAL(triggered()),showWidget->text,SLOT(paste()));
    //关于动作
    aboutAction = new QAction(tr("关于"),this);
    connect(aboutAction,SIGNAL(triggered()),this,SLOT(QApplication::aboutQt()));

    //打印文本的动作
    PrintTextAction = new QAction(QIcon("printText.png"),tr("打印文本"),this);
    PrintTextAction->setStatusTip(tr("打印一个文本"));
    connect(PrintTextAction,SIGNAL(triggered()),this,SLOT(ShowPrintText()));
    //打印图片动作
    PrintImageAction = new QAction(QIcon("printImage.png"),tr("打印图片"),this);
    PrintImageAction->setStatusTip(tr("打印一副图片"));
    connect(PrintImageAction,SIGNAL(triggered()),this,SLOT(ShowPrintImage()));
    //放大动作
    zoomInAction = new QAction(QIcon("zoomin.png"),tr("放大"),this);
    zoomInAction->setStatusTip(tr("放大一副图片"));
    connect(zoomInAction,SIGNAL(triggered()),this,SLOT(ShowZoomIn()));
    //缩小动作
    zoomOutAction = new QAction(QIcon("zoomout.png"),tr("缩小"),this);
    zoomOutAction->setStatusTip(tr("缩小一副图片"));
    connect(zoomOutAction,SIGNAL(triggered()),this,SLOT(ShowZoomOut()));
    //实现图片旋转的动作
    //旋转90°
    rotate90Action = new QAction(QIcon("rotate90.png"),tr("旋转90°"),this);
    rotate90Action->setStatusTip(tr("将一副图旋转90°"));
    connect(rotate90Action,SIGNAL(tiggered()),this,SLOT(ShowRotate90()));
    //旋转180°
    rotate180Action = new QAction(QIcon("rotate180.png"),tr("旋转180°"),this);
    rotate180Action->setStatusTip(tr("将一幅图旋转180°"));
    //旋转270°
    rotate270Action = new QAction(QIcon("rotate270.png"),tr("旋转270°"),this);
    rotate270Action->setStatusTip(tr("将一幅图旋转270°"));
    //实现图片镜像的动作(Action)
    //纵向镜像
    mirrorVecticalAction = new QAction(QIcon("mirrorVertical.png"),tr("纵向镜像"),this);
    mirrorVecticalAction->setStatusTip(tr("对一幅图做纵向镜像"));
    //横向镜像
    mirrorHorizontalAction = new QAction(QIcon("mirrorHorizontal.png"),tr("横向镜像"),this);
    mirrorHorizontalAction->setStatusTip(tr("对一副图做横向镜像"));

    //实现撤销和重做的动作(QAction)
    //撤销和重做
    undoAction = new QAction(QIcon("undo.png"),"撤销",this);
    connect(undoAction,SIGNAL(triggered()),showWidget->text,SLOT(undo()));

    redoAction = new QAction(QIcon("redo.png"),"重做",this);
    connect(redoAction,SIGNAL(triggered()),showWidget->text,SLOT(redo()));
}

void ImgProcessor::createMenus()
{
    //文件菜单
    fileMenu = menuBar()->addMenu(tr("文件"));
    fileMenu->addAction(openFileAction);
    fileMenu->addAction(NewFileAction);
    fileMenu->addAction(PrintTextAction);
    fileMenu->addAction(PrintImageAction);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAction);

    //缩放菜单
    zoomMenu = menuBar()->addMenu(tr("缩放"));
    zoomMenu->addAction(copyAction);
    zoomMenu->addAction(cutAction);
    zoomMenu->addAction(pasteAction);
    zoomMenu->addAction(aboutAction);
    zoomMenu->addSeparator();
    zoomMenu->addAction(zoomInAction);
    zoomMenu->addAction(zoomOutAction);

    //旋转菜单
    rotateMenu = menuBar()->addMenu(tr("旋转"));
    rotateMenu->addAction(rotate90Action);
    rotateMenu->addAction(rotate180Action);
    rotateMenu->addAction(rotate270Action);
    //镜像菜单
    mirrorMenu = menuBar()->addMenu(tr("镜像"));
    mirrorMenu->addAction(mirrorVecticalAction);
    mirrorMenu->addAction(mirrorHorizontalAction);
}
void ImgProcessor::createToolBars()
{
    //文件工具条
    fileTool = addToolBar("File");
    fileTool->addAction(openFileAction);
    fileTool->addAction(NewFileAction);
    fileTool->addAction(PrintTextAction);
    fileTool->addAction(PrintImageAction);

    //编辑工具条
    zoomTool = addToolBar("Edit");
    zoomTool->addAction(copyAction);
    zoomTool->addAction(cutAction);
    zoomTool->addAction(cutAction);
    zoomTool->addAction(pasteAction);
    zoomTool->addSeparator();
    zoomTool->addAction(zoomInAction);
    zoomTool->addAction(zoomOutAction);
    //旋转工具条
    rotateTool = addToolBar("rotate");
    rotateTool->addAction(rotate90Action);
    rotateTool->addAction(rotate180Action);
    rotateTool->addAction(rotate270Action);
    //撤销和重做
    doToolBar = addToolBar("doEdit");
    doToolBar->addAction(undoAction);
    doToolBar->addAction(redoAction);
}

void ImgProcessor::loadFile(QString filename)
{
    printf("file name:%s\n",filename.data());
    QFile file(filename);
    if(file.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        QTextStream textStream(&file);
        while(!textStream.atEnd())
        {
            showWidget->text->append(textStream.readLine());
            printf("read line\n");
        }
        printf("end\n");
    }
}

void ImgProcessor::mergeFormat(QTextCharFormat)
{

}

void ImgProcessor::ShowNewFile()
{
    ImgProcessor* newImgProcessor = new ImgProcessor;
    newImgProcessor->show();
}

void ImgProcessor::ShowOpenFile()
{
    fileName = QFileDialog::getOpenFileName(this);
    if(!fileName.isEmpty())
    {
        if(showWidget->text->document()->isEmpty())
        {
            loadFile(fileName);
        }
        else
        {
            ImgProcessor* newImgProcessor = new ImgProcessor;
            newImgProcessor->show();
            newImgProcessor->loadFile(fileName);
        }
    }

}

void ImgProcessor::ShowPrintText()
{
    QPrinter printer;
    QPrintDialog printDialog(&printer,this);
    if(printDialog.exec())
    {
        QTextDocument* doc = showWidget->text->document();
        doc->print(&printer);
    }

}

void ImgProcessor::ShowPrintImage()
{
    QPrinter printer;//打印设置
    QPrintDialog printDialog(&printer,this);
    if(printDialog.exec())//如果返回true，打印设置也会一起带进来
    {
        QPainter painter(&printer);//把图片画进printer
        QRect rect = painter.viewport();//获取QPainter对象的视图矩形区域
        QSize size = img.size();//获取图像的大小
        /*按照图像的比例大小重新设置视图矩形区域*/
        size.scale(rect.size(),Qt::KeepAspectRatio);
        painter.setViewport(rect.x(),rect.y(),size.width(),size.height());
        painter.setWindow(img.rect());
        painter.drawImage(0,0,img);//打印图像
    }
}

void ImgProcessor::ShowZoomIn()
{
    if(img.isNull())
        return;
    QMatrix matrix;
    matrix.scale(2,2);
    img = img.transformed(matrix);
    showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

void ImgProcessor::ShowZoomOut()
{
    if(img.isNull())
        return;
    QMatrix matrix;
    matrix.scale(0.5,0.5);
    img = img.transformed(matrix);
    showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
}

void ImgProcessor::ShowRotate90()
{

}

